<?php
namespace app\admin\controller;

use app\common\model\Administrator as M_User;
use app\common\model\AdministratorLog;
use app\common\validate\Administrator as V_User;
use bigDream\thinkJump\Jump;
use think\facade\Request;

/**
 * 管理员管理
 * @package app\admin\controller
 * @author jwj <jwjbjg@gmail.com>
 * @copyright jwj
 */
class Administrator
{
    public function __construct()
    {
        if (1 != app('Administrator')['id']) Jump::error('无权访问');
    }

    /**
     * 列表
     * @return \think\response\Json|\think\response\View
     */
    public function index()
    {
        if (Request::isGet()) {
            return view();
        }

        $query = M_User::autoSearch()->order('id', 'desc');

        $result = $query->paginate(Request::param('limit', 20));

        return json([
            'code'  => 0,
            'msg'   => '获取成功',
            'count' => $result->total(),
            'data'  => $result->items()
        ]);
    }

    /**
     * 添加
     * @return \think\response\View
     */
    public function add()
    {
        if (!Request::isAjax()) {
            return view('edit', ['result' => '{}']);
        }

        $validate = (new V_User())->scene('add');
        $data = Request::only($validate->getSceneField());
        if (!$validate->check($data)) {
            Jump::error($validate->getError());
        }

        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        $data['token'] = '';

        M_User::startTrans();
        try {
            $data['token'] = '';
            $result = M_User::create($data);

            M_User::commit();
        } catch (\Exception $e) {
            M_User::rollback();

            Jump::error('发生错误:' . $e->getMessage());
        }

        Jump::success('添加成功');
    }

    /**
     * 编辑
     * @return \think\response\View
     */
    public function edit()
    {
        $result = M_User::where('id', Request::param('id'))->find();
        if (null === $result) {
            Jump::error('内容不存在');
        }

        if (!Request::isAjax()) {
            return view('', [
                'result' => $result,
            ]);
        }

        $validate = (new V_User())->scene('edit');
        $data = Request::only($validate->getSceneField());
        if (!$validate->check($data)) {
            Jump::error($validate->getError());
        }

        if (isset($data['password']) && '' == $data['password']) unset($data['password']);

        M_User::startTrans();
        try {
            $result->save($data);

            M_User::commit();
        } catch (\Exception $e) {
            M_User::rollback();

            Jump::error('发生错误:' . $e->getMessage());
        }

        Jump::success('修改成功');
    }

    /**
     * 删除
     */
    public function delete()
    {
        M_User::startTrans();
        try {
            $result = M_User::where('id', 'in', Request::param('id'))->select();
            foreach ($result as $res) {
                if (1 == $res['id']) continue;
                $res->delete();
            }

            M_User::commit();
        } catch (\Exception $e) {
            M_User::rollback();

            Jump::error($e->getMessage());
        }

        Jump::success('删除成功');
    }

    /**
     * 用户日志
     * @return mixed|\think\response\Json
     */
    public function log()
    {
        if (!Request::isAjax()) {
            return view();
        }

        $query = AdministratorLog::autoSearch()
            ->order('id', 'desc');

        $result = $query->paginate(Request::param('limit'));

        return json([
            'code'  => 0,
            'msg'   => '获取成功',
            'count' => $result->total(),
            'data'  => $result->items()
        ]);
    }
}
